import Vue from 'vue'
import VueRouter from 'vue-router'
// import HomeView from '../views/HomeView.vue'
import Login from '../components/login.vue'
import Home from '../components/home.vue'

import Onlineclass from '../components/container/onlineclass.vue'
import loadppt from '../components/container/loadppt.vue'
import discuss from '../components/container/discuss.vue'
import testWork from '../components/container/testWork.vue'
import worktest1 from '../components/WorkTest/worktest1.vue'
import worktest2 from '../components/WorkTest/worktest2.vue'
import worktest1View from '@/components/WorkTest/worktest1View.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login'},
  { path: '/login', component: Login},
  { path: '/home',
    component: Home,
    children:[
        { path: '/', redirect: '/onlineclass'},//一进入就打开这个页面
        { path: '/onlineclass', component: Onlineclass},
        { path: '/loadppt', component: loadppt},
        { path: '/discuss', component: discuss},
        { path: '/testWork', component: testWork},
        { path: '/worktest1/:id', component: worktest1},
        { path: '/worktest1View/:id', component: worktest1View},
        { path: '/worktest2/:id', component: worktest2}
      ]
  },
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转

  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
